package com.zqcy.workbench.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.shinemo.base.core.utils.ToastUtil;
import com.zqcy.workbench.R;
import com.zqcy.workbench.adapter.AddTabAdapter;
import com.zqcy.workbench.adapter.AllTabAdapter;
import com.zqcy.workbench.adapter.AddTabImageAdapter;
import com.zqcy.workbench.bean.RefreshEvent;
import com.zqcy.workbench.bean.TabConfiguration;
import com.zqcy.workbench.utils.Utils;
import com.zqcy.workbench.utils.Preferences;
import com.zqcy.workbench.utils.RxBus;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import static com.zqcy.workbench.utils.Constants.TAB_CONFIGURATION;

/**
 * tab编辑页面
 */
public class ConfigurationEditActivity extends AppCompatActivity {

    private ImageView all_application_back; //返回
    private TextView all_application_edit_cancel; //取消
    private TextView all_application_edit_finish; //完成
    private RecyclerView all_application_edit_view; //可编辑视图
    private RelativeLayout all_application_added_view; //不可编辑视图
    private TextView all_application_addedTip; //已添加动态文案
    private RecyclerView all_application_added_rc; // 已添加图标
    private TextView all_application_edit; //编辑按钮
    private RecyclerView all_application_rc; //底部view

    private AllTabAdapter allTabAdapter; //全部应用适配器
    private AddTabImageAdapter addTabImageAdapter;//已添加image适配器
    private AddTabAdapter addTabAdapter; //已添加item适配器


    private boolean isEdit = false; //是否处于编辑状态

    private List<TabConfiguration> tabConfigurationList = new ArrayList<>(); //已添加的list集合
    private List<TabConfiguration> allList = new ArrayList<>();//全部应用list集合

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, ConfigurationEditActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_configuration_edit);
        Utils.setColor(this, getResources().getColor(R.color.white));
        initView();
        initListener();
        tabConfigurationList.clear();
        tabConfigurationList = Preferences.getDataList(ConfigurationEditActivity.this, TAB_CONFIGURATION, TabConfiguration.class);
        initData();

        initEdit();
        initAllList();

    }

    private void initView() {
        all_application_back = findViewById(R.id.all_application_back);
        all_application_edit_cancel = findViewById(R.id.all_application_edit_cancel);
        all_application_edit_finish = findViewById(R.id.all_application_edit_finish);
        all_application_edit_view = findViewById(R.id.all_application_edit_view);
        all_application_added_view = findViewById(R.id.all_application_added_view);
        all_application_addedTip = findViewById(R.id.all_application_addedTip);
        all_application_added_rc = findViewById(R.id.all_application_added_rc);
        all_application_edit = findViewById(R.id.all_application_edit);
        all_application_rc = findViewById(R.id.all_application_rc);
    }

    /**
     * 设置编辑/不可编辑状态view
     */
    @SuppressLint("SetTextI18n")
    private void initEdit() {
        if (isEdit) {
            all_application_edit_view.setVisibility(View.VISIBLE);
            all_application_added_view.setVisibility(View.GONE);
            all_application_back.setVisibility(View.GONE);
            all_application_edit_cancel.setVisibility(View.VISIBLE);
            all_application_edit_finish.setVisibility(View.VISIBLE);

            addTabAdapter = new AddTabAdapter(this, initEmptyList(tabConfigurationList));
            GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 5);
            all_application_edit_view.setLayoutManager(gridLayoutManager);
            all_application_edit_view.setAdapter(addTabAdapter);
        } else {
            all_application_edit_view.setVisibility(View.GONE);
            all_application_added_view.setVisibility(View.VISIBLE);
            all_application_back.setVisibility(View.VISIBLE);
            all_application_edit_cancel.setVisibility(View.GONE);
            all_application_edit_finish.setVisibility(View.GONE);

            all_application_addedTip.setText("已添加(" + tabConfigurationList.size() + ")");
            addTabImageAdapter = new AddTabImageAdapter(this, tabConfigurationList);
            LinearLayoutManager layoutManager = new LinearLayoutManager(this) {
                @Override
                public boolean canScrollHorizontally() {
                    return false;//禁止滑动
                }
            };
            layoutManager.setOrientation(LinearLayoutManager.HORIZONTAL);
            all_application_added_rc.setLayoutManager(layoutManager);
            all_application_added_rc.setAdapter(addTabImageAdapter);
        }
    }

    /**
     * 设置可编辑list集合
     *
     * @param tabConfigurationList
     */
    private List<TabConfiguration> initEmptyList(List<TabConfiguration> tabConfigurationList) {
        List<TabConfiguration> list = new ArrayList<>(tabConfigurationList);
        for (int i = 0; i < 9 - tabConfigurationList.size(); i++) {
            //循环塞入空数据
            TabConfiguration tabConfiguration = new TabConfiguration();
            tabConfiguration.setTabId(99);
            list.add(tabConfiguration);
        }
        return list;
    }

    /**
     * 设置全部应用list
     */
    private void initAllList() {
        if (isEdit) {
            if (tabConfigurationList.size() > 0) {
                for (TabConfiguration tabConfiguration : allList) {
                    for (TabConfiguration configuration : tabConfigurationList) {
                        if (tabConfiguration.getTabId() == configuration.getTabId()) {
                            tabConfiguration.setAdd(true);
                        }
                    }
                }
            }
        }
        allTabAdapter = new AllTabAdapter(this, allList, isEdit);
        GridLayoutManager gridLayoutManager = new GridLayoutManager(this, 5);
        all_application_rc.setLayoutManager(gridLayoutManager);
        all_application_rc.setAdapter(allTabAdapter);

        allTabAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull @NotNull BaseQuickAdapter<?, ?> adapter, @NonNull @NotNull View view, int position) {
                if (isEdit) {
                    TabConfiguration item = (TabConfiguration) adapter.getData().get(position);
                    if (item.isAdd() && tabConfigurationList.size() <= 9 || !item.isAdd() && tabConfigurationList.size() <= 8) {
                        if (item.isAdd()) {
                            //已被添加
                            item.setAdd(false);
                            Iterator<TabConfiguration> iterator = tabConfigurationList.iterator();
                            while (iterator.hasNext()) {
                                TabConfiguration tabConfiguration = iterator.next();
                                if (tabConfiguration.getTabId() == item.getTabId()) {
                                    iterator.remove();
                                }
                            }
                        } else {
                            //还没被添加
                            item.setAdd(true);
                            tabConfigurationList.add(item);
                        }
                        initEdit();
                        allTabAdapter.notifyDataSetChanged();
                    }
                }
            }
        });
    }

    private void initData() {
        allList.clear();
        TabConfiguration tabConfiguration1 = new TabConfiguration();
        tabConfiguration1.setTabIcon(R.mipmap.tab_manage_money);
        tabConfiguration1.setTabName(R.string.manage_money);
        tabConfiguration1.setTabId(1);
        tabConfiguration1.setAdd(false);
        allList.add(tabConfiguration1);

        TabConfiguration tabConfiguration2 = new TabConfiguration();
        tabConfiguration2.setTabIcon(R.mipmap.tab_fund);
        tabConfiguration2.setTabName(R.string.fund);
        tabConfiguration2.setTabId(2);
        tabConfiguration2.setAdd(false);
        allList.add(tabConfiguration2);

        TabConfiguration tabConfiguration3 = new TabConfiguration();
        tabConfiguration3.setTabIcon(R.mipmap.tab_gold);
        tabConfiguration3.setTabName(R.string.gold);
        tabConfiguration3.setTabId(3);
        tabConfiguration3.setAdd(false);
        allList.add(tabConfiguration3);

        TabConfiguration tabConfiguration4 = new TabConfiguration();
        tabConfiguration4.setTabIcon(R.mipmap.tab_insurance);
        tabConfiguration4.setTabName(R.string.insurance);
        tabConfiguration4.setTabId(4);
        tabConfiguration4.setAdd(false);
        allList.add(tabConfiguration4);

        TabConfiguration tabConfiguration5 = new TabConfiguration();
        tabConfiguration5.setTabIcon(R.mipmap.tab_finance);
        tabConfiguration5.setTabName(R.string.cross_border_finance);
        tabConfiguration5.setTabId(5);
        tabConfiguration5.setAdd(false);
        allList.add(tabConfiguration5);

        TabConfiguration tabConfiguration6 = new TabConfiguration();
        tabConfiguration6.setTabIcon(R.mipmap.tab_transfer);
        tabConfiguration6.setTabName(R.string.transfer);
        tabConfiguration6.setTabId(6);
        tabConfiguration6.setAdd(false);
        allList.add(tabConfiguration6);

        TabConfiguration tabConfiguration7 = new TabConfiguration();
        tabConfiguration7.setTabIcon(R.mipmap.tab_product);
        tabConfiguration7.setTabName(R.string.product_contract);
        tabConfiguration7.setTabId(7);
        tabConfiguration7.setAdd(false);
        allList.add(tabConfiguration7);

        TabConfiguration tabConfiguration8 = new TabConfiguration();
        tabConfiguration8.setTabIcon(R.mipmap.tab_plan);
        tabConfiguration8.setTabName(R.string.wealth_planning);
        tabConfiguration8.setTabId(8);
        tabConfiguration8.setAdd(false);
        allList.add(tabConfiguration8);

        TabConfiguration tabConfiguration9 = new TabConfiguration();
        tabConfiguration9.setTabIcon(R.mipmap.tab_location_search);
        tabConfiguration9.setTabName(R.string.location_search);
        tabConfiguration9.setTabId(9);
        tabConfiguration9.setAdd(false);
        allList.add(tabConfiguration9);

        TabConfiguration tabConfiguration10 = new TabConfiguration();
        tabConfiguration10.setTabIcon(R.mipmap.tab_account);
        tabConfiguration10.setTabName(R.string.account_overview);
        tabConfiguration10.setTabId(10);
        tabConfiguration10.setAdd(false);
        allList.add(tabConfiguration10);

        TabConfiguration tabConfiguration11 = new TabConfiguration();
        tabConfiguration11.setTabIcon(R.mipmap.tab_deposit);
        tabConfiguration11.setTabName(R.string.deposit);
        tabConfiguration11.setTabId(11);
        tabConfiguration11.setAdd(false);
        allList.add(tabConfiguration11);

        TabConfiguration tabConfiguration12 = new TabConfiguration();
        tabConfiguration12.setTabIcon(R.mipmap.tab_high_end_equity);
        tabConfiguration12.setTabName(R.string.high_end_equity);
        tabConfiguration12.setTabId(12);
        tabConfiguration12.setAdd(false);
        allList.add(tabConfiguration12);

        TabConfiguration tabConfiguration13 = new TabConfiguration();
        tabConfiguration13.setTabIcon(R.mipmap.tab_bond);
        tabConfiguration13.setTabName(R.string.bond);
        tabConfiguration13.setTabId(13);
        tabConfiguration13.setAdd(false);
        allList.add(tabConfiguration13);

        TabConfiguration tabConfiguration14 = new TabConfiguration();
        tabConfiguration14.setTabIcon(R.mipmap.tab_borrow);
        tabConfiguration14.setTabName(R.string.borrow_money);
        tabConfiguration14.setTabId(14);
        tabConfiguration14.setAdd(false);
        allList.add(tabConfiguration14);

        TabConfiguration tabConfiguration15 = new TabConfiguration();
        tabConfiguration15.setTabIcon(R.mipmap.tab_exchange);
        tabConfiguration15.setTabName(R.string.foreign_exchange);
        tabConfiguration15.setTabId(15);
        tabConfiguration15.setAdd(false);
        allList.add(tabConfiguration15);

        TabConfiguration tabConfiguration16 = new TabConfiguration();
        tabConfiguration16.setTabIcon(R.mipmap.tab_extra_deposit);
        tabConfiguration16.setTabName(R.string.extra_deposit);
        tabConfiguration16.setTabId(16);
        tabConfiguration16.setAdd(false);
        allList.add(tabConfiguration16);

        TabConfiguration tabConfiguration17 = new TabConfiguration();
        tabConfiguration17.setTabIcon(R.mipmap.tab_turnover);
        tabConfiguration17.setTabName(R.string.turnover_printing);
        tabConfiguration17.setTabId(17);
        tabConfiguration17.setAdd(false);
        allList.add(tabConfiguration17);

        TabConfiguration tabConfiguration18 = new TabConfiguration();
        tabConfiguration18.setTabIcon(R.mipmap.tab_invoice);
        tabConfiguration18.setTabName(R.string.invoice);
        tabConfiguration18.setTabId(18);
        tabConfiguration18.setAdd(false);
        allList.add(tabConfiguration18);

        TabConfiguration tabConfiguration19 = new TabConfiguration();
        tabConfiguration19.setTabIcon(R.mipmap.tab_bank_card);
        tabConfiguration19.setTabName(R.string.bank_card);
        tabConfiguration19.setTabId(19);
        tabConfiguration19.setAdd(false);
        allList.add(tabConfiguration19);

        TabConfiguration tabConfiguration20 = new TabConfiguration();
        tabConfiguration20.setTabIcon(R.mipmap.tab_details);
        tabConfiguration20.setTabName(R.string.exchange_details);
        tabConfiguration20.setTabId(20);
        tabConfiguration20.setAdd(false);
        allList.add(tabConfiguration20);
    }


    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    private void initListener() {
        all_application_edit_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isEdit = false;
                tabConfigurationList.clear();
                tabConfigurationList = Preferences.getDataList(ConfigurationEditActivity.this, TAB_CONFIGURATION, TabConfiguration.class);
                initData();

                initEdit();
                initAllList();

            }
        });
        all_application_edit_finish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ToastUtil.show(ConfigurationEditActivity.this, "保存成功");
                Preferences.setDataList(ConfigurationEditActivity.this, TAB_CONFIGURATION, tabConfigurationList);
                isEdit = false;
                initData();

                initEdit();
                initAllList();
                RxBus.getInstance().post(new RefreshEvent(true));
            }
        });
        all_application_edit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                isEdit = true;
                initEdit();
                initAllList();
            }
        });
        all_application_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


}